import { getapk } from '@/api/apkApi';
import { getipa } from '@/api/ipaApi';
import { getmiao } from '@/api/miaoApi';
import { getsuper } from '@/api/superApi';
import { $message } from '@/components/AntDesign/message';
import { AxiosResponse } from 'axios';
import { TabledataInterface, TypeEnum } from './types';
export * from './types';

export interface TabledatasInterface {
    tabledata: TabledataInterface[]
    total: number
}

export const TableData = (type: TypeEnum, current: number = 1, id?: string | null, pageSize: number = 6): Promise<TabledatasInterface> => {
    return new Promise(async (res, rej) => {
        const Tabledatas: TabledatasInterface = {
            tabledata: [],
            total: 0
        };
        try {
            let dataLsit: AxiosResponse | null = null
            switch (type) {
                case TypeEnum.superModel:
                    dataLsit = await getsuper({ pageNo: current, pageSize, id });
                    break;
                case TypeEnum.apkModel:
                    dataLsit = await getapk({ pageNo: current, pageSize, id });
                    break;
                case TypeEnum.iosModel:
                    dataLsit = await getipa({ pageNo: current, pageSize, id });
                    break;
                case TypeEnum.miaoModel:
                    dataLsit = await getmiao({ pageNo: current, pageSize, id });
                    break;
            }

            const reslut: TabledataInterface[] = TableForEach(dataLsit.data.data)
            Tabledatas.total = dataLsit.data.total;
            Tabledatas.tabledata = reslut;
            res(Tabledatas)
        } catch (error) {
            $message("error", error.data.message);
            rej(error)
        }
    })
}

export const TableForEach = (reslut: TabledataInterface[]) => {
    reslut.forEach((element: TabledataInterface) => {
        element.key = element._id;
        element.appSize = `${element.appSize}M`;
        element.iconPath = `${(window as any).$URL}/${element.iconPath}`;
        element.appPath = `${(window as any).$URL}/${element.appPath}`;
    });
    return reslut
}

